package com.lcu.bos.web.action;

import com.lcu.bos.modle.User;
import com.lcu.bos.service.IUserService;
import com.lcu.bos.untils.MD5Utils;
import com.lcu.bos.web.action.base.BaseAction;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class UserAction extends BaseAction<User> {//通过父类的泛型明确模块的Modle
    @Autowired
    private IUserService userService;
    //创建一个日志对象
    Logger logger=Logger.getLogger(UserAction.class);

    //实现登陆
    public  String login(){
        logger.info(getModel());
        logger.error(getModel());
        logger.warn(getModel());

        String username=getModel().getUsername();
        String password=getModel().getPassword();
        HttpServletRequest request = ServletActionContext.getRequest();
        String checkcode = request.getParameter("checkcode");
        HttpSession session = request.getSession();
        String servletCheckCode = (String) session.getAttribute("key");
        session.removeAttribute("key");//防止返回重新登入
        if(servletCheckCode!=null&&servletCheckCode.equalsIgnoreCase(checkcode)){
            //验证码正确
            //调用service
            //使用shiro,就不需要使用userService的login方法                            User user = userService.login(username, password);
            /*使用shiro中的subject方法*/
            Subject subject= SecurityUtils.getSubject();
            //创建一个token,这里面存着用户名和密码
            UsernamePasswordToken token=new UsernamePasswordToken(username, MD5Utils.text2md5(password));
            try {
                subject.login(token);
                //登录成功,把对象存入在session中
                User loginUser=(User)subject.getPrincipal();
                subject.getSession().setAttribute("loginUser",loginUser);
                return "home";
            }catch (AuthenticationException e){
                e.printStackTrace();
                addActionError("登录失败，用户名或密码不正确");
            }
            //判断登入状态
        }else {
            addActionError("验证码输入错误");
            //System.out.println("验证码输入错误");
        }
        return "loginErr";
    }
    public String logout(){
        //退出的话就把session中的数据清除掉
        ServletActionContext.getRequest().getSession().invalidate();
        return "login";
    }
    public String editPassword() throws IOException {
        //1.获取新密码
        String password = getModel().getPassword();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        //2.获取用户id
        User user = (User) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
        String  id=user.getId();
        //3.调用service
        userService.modifyPassword(password,id);
        //4.返回一个成功信息
        response.setHeader("Content-Type","text/json;charset=utf-8");
        response.getWriter().print("{\"success\":\"1\"}");
        return NONE;
    }
    public String save() {
        return null;
    }

    public String delete() {
        return null;
    }

    public String update() {
        return null;
    }

    public String get() {
        return null;
    }

    public String findAll() {
        return null;
    }
}
